package com.be.beadmin.device.controller;

import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.device.dto.output.EhsStandardEx;
import com.be.beadmin.device.dto.output.EhsStandardExcelEx;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import com.be.beadmin.device.service.EhsStandardService;
import com.be.beadmin.device.dto.input.EhsStandardDto;
import com.be.beadmin.common.utils.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.excel.EasyExcel;

/**
* @author zty
* @date 2023-08-13
**/
@RestController
public class EhsStandardController {

    @Autowired
    private  EhsStandardService ehsStandardService;


    @PostMapping(value = "/device/getEhsStandardByPage")
    public ResultModel getEhsStandardByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(ehsStandardService.getEhsStandardByPage(pageEntity)) ;
    }

    @PostMapping("/device/addEhsStandard")
    public ResultModel addEhsStandard(@RequestBody EhsStandardDto ehsStandardDto){
        ehsStandardService.addEhsStandard(ehsStandardDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/device/delEhsStandard")
    public ResultModel delEhsStandard(@RequestBody EhsStandardDto ehsStandardDto){
        ehsStandardService.delEhsStandard(ehsStandardDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/device/updateEhsStandard")
    public ResultModel updateEhsStandard(@RequestBody EhsStandardDto ehsStandardDto){
        ehsStandardService.updateEhsStandard(ehsStandardDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/device/exportEhsStandard")
    public void exportEhsStandard(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<EhsStandardEx> ehsStandardPage = ehsStandardService.getEhsStandardByPage(pageEntity);
        List<EhsStandardEx> ehsStandardExes = ehsStandardPage.getContent();
        List<EhsStandardExcelEx> ehsStandardExcelExes=ehsStandardExes.stream().map(item->{
            EhsStandardExcelEx excel=new EhsStandardExcelEx();
            BeanUtils.copyProperties(item,excel);
            return excel;
        }).collect(Collectors.toList());
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("EhsStandard接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xls");
        //EasyExcel.write(response.getOutputStream(), EhsStandardExcelEx.class).sheet("sheet1").doWrite(ehsStandardExcelExes);
        ExcelWriterBuilder ewb=EasyExcel.write(response.getOutputStream(), EhsStandardExcelEx.class);
        ewb.excelType(ExcelTypeEnum.XLS);
        ewb.sheet("sheet1").doWrite(ehsStandardExcelExes);
    }

    @ApiOperation(value = "根据templateid获取模板")
    @PostMapping("/device/getEhsStandardByTemplateID")
    public ResultModel getEhsStandardByTemplateID(@RequestParam String TemplateID)  {
        return ResultModel.success(ehsStandardService.getEhsStandardByTemplateID(TemplateID)) ;
    }

    @ApiOperation(value = "获取templateid列表")
    @PostMapping("/device/getTemplateIDs")
    public ResultModel getTemplateIDs()  {
        List<String> TemplateIDs = ehsStandardService.getTemplateIDs();
        return ResultModel.success(TemplateIDs) ;
    }


}